package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpZubieMapper;
import com.ruoyi.system.domain.SpZubie;
import com.ruoyi.system.service.ISpZubieService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 组别Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-10
 */
@Service
public class SpZubieServiceImpl implements ISpZubieService 
{
    @Autowired
    private SpZubieMapper spZubieMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询组别
     * 
     * @param zbId 组别主键
     * @return 组别
     */
    @Override
    public SpZubie selectSpZubieByZbId(Long zbId)
    {
        return spZubieMapper.selectSpZubieByZbId(zbId);
    }

    /**
     * 查询组别列表
     * 
     * @param spZubie 组别
     * @return 组别
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpZubie> selectSpZubieList(SpZubie spZubie)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spZubie.setDatascope(datascope);
        if(datascope==4){
            spZubie.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spZubie.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spZubie.setCheckHuiyuanId(0l);
            }

        }
        return spZubieMapper.selectSpZubieList(spZubie);
    }

    @Override
    public List<SpZubie> selectSpZubieListWeb(SpZubie spZubie)
    {
        return spZubieMapper.selectSpZubieList(spZubie);
    }

    /**
     * 新增组别
     * 
     * @param spZubie 组别
     * @return 结果
     */
    @Override
    public int insertSpZubie(SpZubie spZubie)
    {
        LoginUser loguser = SecurityUtils.getLoginUser();
        spZubie.setDeptId(loguser.getDeptId());
        return spZubieMapper.insertSpZubie(spZubie);
    }

    /**
     * 修改组别
     * 
     * @param spZubie 组别
     * @return 结果
     */
    @Override
    public int updateSpZubie(SpZubie spZubie)
    {
        LoginUser loguser = SecurityUtils.getLoginUser();
        spZubie.setDeptId(loguser.getDeptId());
        return spZubieMapper.updateSpZubie(spZubie);
    }

    /**
     * 批量删除组别
     * 
     * @param zbIds 需要删除的组别主键
     * @return 结果
     */
    @Override
    public int deleteSpZubieByZbIds(Long[] zbIds)
    {
        return spZubieMapper.deleteSpZubieByZbIds(zbIds);
    }

    /**
     * 删除组别信息
     * 
     * @param zbId 组别主键
     * @return 结果
     */
    @Override
    public int deleteSpZubieByZbId(Long zbId)
    {
        return spZubieMapper.deleteSpZubieByZbId(zbId);
    }
}
